package com.example.car.controller;

import com.example.car.entity.Car;
import com.example.car.entity.Driver;
import com.example.car.entity.Order;
import com.example.car.model.OrderListModel;
import com.example.car.model.OrderModel;
import com.example.car.service.CarService;
import com.example.car.service.DriverService;
import com.example.car.service.OrderService;
import com.example.car.utils.JwtUtil;
import com.example.car.vo.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.aggregation.BooleanOperators;
import org.springframework.web.bind.annotation.*;

import javax.management.monitor.CounterMonitor;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Api(tags = "订单接口")
@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private DriverService driverService;

    @Autowired
    private CarService carService;

    @GetMapping("list")
    @ApiOperation("订单信息")
    public CommonResult list(OrderParamVo orderParamVo) {
        String userId = orderParamVo.getUserId();
        Integer page = orderParamVo.getPage();
        Integer limit = orderParamVo.getLimit();
        if (StringUtils.isEmpty(userId) || page == null || limit == null) {
            return CommonResult.error("参数异常");
        }
        List<Order> list = orderService.list(userId, page, limit);

        List<OrderListModel> collect = list.stream().map(item -> {
            OrderListModel orderListModel = new OrderListModel();
            CarDirverVo carDirver = item.getCarDirver();
            String carId = carDirver.getCarId();
            Car carById = carService.getCarById(carId);
            BeanUtils.copyProperties(carById, orderListModel);
            orderListModel.setBeaginDate(item.getBeginTime());
            orderListModel.setEndDate(item.getEndDate());
            Integer status = item.getStatus();
            if (status == 1) {
                orderListModel.setStatus("未支付");
            } else {
                orderListModel.setStatus("已支付");
            }
            return orderListModel;
        }).collect(Collectors.toList());

        Long total = orderService.getTotal(userId);

        return CommonResult.success().data("data", collect).data("total", total);
    }

    @PostMapping("addOrder")
    @ApiOperation("下订单")
    public CommonResult addOrder(@RequestBody OrderVo order, HttpServletRequest request) {
        if (order.getRentType() == 2 && order.getDays() < 30) {
            return CommonResult.error("包月租贷天数必须大于等于30 天");
        }
        if (order.getStartTime() == null) {
            order.setStartTime(new Date());
            order.setDays(30);
        }
        String userId = JwtUtil.getUserIdByToken(request);
        String orderId = UUID.randomUUID().toString();
        orderService.addOrder(order, userId, orderId);
        OrderModel orderModel = new OrderModel();
        BeanUtils.copyProperties(order, orderModel);
        if (!StringUtils.isEmpty(order.getDriverId())) {
            Driver driverInfo = driverService.getDriverInfo(order.getDriverId());
            orderModel.setDriverName(driverInfo.getName());
        }
        orderModel.setOrderId(orderId);

        return CommonResult.success().data("data", orderModel);
    }

    @GetMapping("pay/{orderId}")
    @ApiOperation("订单支付")
    public CommonResult payOrder(@PathVariable String orderId) {

        orderService.payOrder(orderId);

        return CommonResult.success();
    }

    @PostMapping("order/statistics")
    @ApiOperation("时间段内订单总金额，默认当天")
    public CommonResult orderStatistics(@RequestBody TimeConditionVo timeConditionVo) {
        Double money = orderService.orderStatistics(timeConditionVo);
        return CommonResult.success().data("data", money);
    }

}

